import pymysql
import concurrent.futures
from erp_fangling_cn import owner


def batchInsertOwner(houseParam, houses):
    if len(houses) == 0:
        return

    # 数据库连接
    conn = pymysql.connect(
        host="43.156.247.50",  # 替换为实际的主机名
        user="root",  # 替换为实际的用户名
        password="520777",  # 替换为实际的密码
        database="house"  # 替换为实际的数据库名
    )
    cursor = conn.cursor()

    # 循环每个房产
    for house in houses:
        # 查询单个房产的 owner 信息
        house_id = house["house_id"]
        owners = owner.getOwner(houseParam, house_id)

        # 存在则循环，并且入库
        if len(owners) > 0:
            for o in owners:
                sql = "INSERT INTO house_owner (owner_id, house_id, owner_name, tel, cityId) VALUES (%s, %s, %s, %s, %s)"
                values = (
                    o['ownerId'],
                    house_id,
                    o['ownerName'],
                    o['tel'],
                    o['cityId'],
                )

                cursor.execute(sql, values)

            # 每个房产提交一次
            conn.commit()

    cursor.close()
    conn.close()
